package com.hy.dao.impl;
/*
 * @ Date: 2024/5/14 22:27
 * @ Description: BuyParttableDaoImpl
 **/

import com.hy.bean.BuyParttable;
import com.hy.dao.BuyParttableDao;
import com.hy.exception.BizException;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BuyParttableDaoImpl implements BuyParttableDao {
    private QueryRunner queryRunner = new QueryRunner();
    private static BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());


    @Override
    public BuyParttable queryById(Integer id) {
        return null;
    }

    @Override
    public List<BuyParttable> queryAllByLimit() {
        return null;
    }

    @Override
    public long count(BuyParttable buyParttable) {
        return 0;
    }

    /***
     * 新增采购明细数据
     * @param connection 链接
     * @param buyParttable 实例对象
     * @return
     */
    @Override
    public int insert(Connection connection,BuyParttable buyParttable) {
        String sql = "INSERT INTO `automobile_sales_module`.`buy_parttable` (`id`, `buyTable_id`, `partTable_id`, `buyCount`, `inStoreCount`, `reachCount`, `factoryRebate`, `totalPrice`) VALUES (null, ?, ?, ?, ?, ?, ?, ?)";
        try {
            BigInteger integer = queryRunner.insert(connection, sql, new ScalarHandler<>(),buyParttable.getBuytableId(), buyParttable.getParttableId(), buyParttable.getBuycount(), buyParttable.getInstorecount(), buyParttable.getReachcount(), buyParttable.getFactoryrebate(), buyParttable.getTotalprice());
            return integer.intValue();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(BuyParttable buyParttable) {
        return 0;
    }

    @Override
    public int updateReachCountById(Connection connection, Integer id, Integer reachCount){
        String sql = "update buy_parttable set reachCount = ? , inStoreCount = ? where id = ?";
        try {
            return queryRunner.update(connection, sql, reachCount,reachCount,id);
        } catch (SQLException e) {
            throw new BizException("更新采购中间表失败",e);
        }
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }

    @Override
    public List<BuyParttable> queryAll() {
        Connection connection = null;
        String sql = "SELECT * FROM buy_parttable buyp join buytable buy on buyp.buyTable_id = buy.id where buy.requestTime = DATE_FORMAT(NOW(),'%Y-%m-%d') and buy.state = '2'";
        try {
            connection = DBManager.getConnection();
            return queryRunner.query(connection, sql, new BeanListHandler<>(BuyParttable.class,basicRowProcessor));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public BuyParttable selectByParttableId(Integer id,Integer buytableId){
        Connection connection = null;
       String sql = "select * from buy_parttable where partTable_id = ? and buyTable_id = ?";
       try {
           connection = DBManager.getConnection();
           return queryRunner.query(connection, sql, new BeanHandler<>(BuyParttable.class,basicRowProcessor),id,buytableId);
       } catch (Exception e) {
           throw new RuntimeException(e);
       }finally {
           try {
               DbUtils.close(connection);
           } catch (SQLException e) {
               throw new RuntimeException(e);
           }
       }
    }

    @Override
    public int updateBuycount(Connection connection, Integer id, Integer addedInventoryQuantity) {
        String sql = "update buy_parttable set buycount = ? where id = ?";
        try {
            return queryRunner.update(connection, sql, addedInventoryQuantity, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Double selectsumzj(Integer id) {
        Connection connection = null;
        String sql = "select SUM(part.totalPrice)zje from buy_parttable part join buytable buy on part.buyTable_id = buy.id where buy.id = ?";
        try {
            connection = DBManager.getConnection();
            return queryRunner.query(connection, sql, new ScalarHandler<>(1), id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

